﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
namespace JiveMessenger.Helpers
{
    public static class ExtensionMethods
    {
        public static void EnqueueWithCapacity<T>(this Queue<T> q, T item, int MaxSize)
        {
            if (q.Count >= MaxSize) q.Dequeue();
            q.Enqueue(item);
        }

        public static void AddRange<TK, TS>(this Dictionary<TK, TS> source, Dictionary<TK, TS> collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException("collection");
            }

            foreach (var item in collection)
            {
                if (!source.ContainsKey(item.Key))
                {
                    source.Add(item.Key, item.Value);
                }
            }
        }

        public static List<int> AllIndexesOf(this string str, string value)
        {
            if (String.IsNullOrEmpty(value))
                throw new ArgumentException("the string to find may not be empty", "value");
            List<int> indexes = new List<int>();
            for (int index = 0; ; index += value.Length)
            {
                index = str.IndexOf(value, index);
                if (index == -1)
                    return indexes;
                indexes.Add(index);
            }
        }

        public static string TrimStart(this string source, string value)
        {
            if (!source.StartsWith(value))
                return source;

            return source.Remove(source.IndexOf(value, StringComparison.Ordinal), value.Length);
        }
        public static bool InvokeRequired(this FrameworkElement element)
        {
            return !element.Dispatcher.CheckAccess();
        }
    }

}